package love.bot.forli.group.review.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import love.bot.forli.group.review.bean.Result;
import love.bot.forli.group.review.entity.Code;
import love.bot.forli.group.review.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 入群码控制器
 *
 * @author Kontori
 */
@Api(tags = "入群码相关接口")
@RestController
@RequestMapping("/code")
@Transactional(rollbackFor = {Throwable.class})
public class CodeController {

    private CodeService codeService;

    @Autowired
    public void setCodeService(CodeService codeService) {
        this.codeService = codeService;
    }

    @ApiOperation("所有入群码")
    @GetMapping("/list")
    public Result<List<Code>> list(@ApiParam(value = "页码") @RequestParam Integer page,
                                   @ApiParam(value = "数量") @RequestParam Integer limit,
                                   @ApiParam(value = "QQ号搜索") @RequestParam String qq) {
        return Result.success(codeService.page(new Page<>(page, limit), new QueryWrapper<Code>()
                .like("qq", qq)).getRecords());
    }

    @ApiOperation("创建入群码")
    @PostMapping("/create")
    public Result<String> create(@ApiParam(value = "使用者QQ", required = true) @RequestBody Map<String, Object> map) {
        return codeService.createCode((String) map.get("qq"));
    }

    @ApiOperation("修改入群码")
    @PostMapping("/edit")
    public Result<String> edit(@RequestBody Code code) {
        return codeService.updateById(code) ? Result.success("修改成功", null) : Result.fail("修改失败");
    }

    @ApiOperation("删除入群码记录")
    @DeleteMapping("/delete/{codeId}")
    public Result<String> delete(@PathVariable("codeId") Integer codeId) {
        return codeService.removeById(codeId) ? Result.success("删除成功", null) : Result.fail("删除失败");
    }

    @ApiOperation("获取入群码总量")
    @GetMapping("/pageSize")
    public Result<Long> pageSize(@ApiParam(value = "QQ号搜索") @RequestParam String qq) {
        return Result.success(codeService.count(new QueryWrapper<Code>()
                .like("qq", qq)));
    }

    @ApiOperation("获取指定入群码信息")
    @GetMapping("/get")
    public Result<Code> get(@ApiParam(value = "入群码", required = true) @RequestParam String code) {
        return Result.success(codeService.getOne(Wrappers.lambdaQuery(Code.class).eq(Code::getValue, code)));
    }

    @ApiOperation("标记入群码为已使用")
    @GetMapping("/use")
    public Result<Object> use(@ApiParam(value = "入群码", required = true) @RequestParam String code) {
        return codeService.update(Wrappers.lambdaUpdate(Code.class)
                .eq(Code::getValue, code)
                .set(Code::getStatus, 1)) ?
                Result.success("修改成功", null) : Result.fail("修改失败");
    }

}
